import { _decorator, Component, Node, Button, director } from 'cc';
import { AudioManager } from '../core/AudioManager';
const { ccclass, property } = _decorator;

/**
 * 主菜单UI控制脚本
 */
@ccclass('MainMenuUI')
export class MainMenuUI extends Component {
    @property(Button)
    private startGameButton: Button = null;
    
    @property(Button)
    private settingsButton: Button = null;
    
    @property(Button)
    private aboutButton: Button = null;
    
    @property(Button)
    private quitButton: Button = null;
    
    @property(Node)
    private settingsPanel: Node = null;
    
    @property(Node)
    private aboutPanel: Node = null;
    
    @property(String)
    private gameSceneName: string = 'GameScene';
    
    @property(AudioManager)
    private audioManager: AudioManager = null;
    
    onLoad() {
        console.log('MainMenuUI onLoad');
        this.initUI();
        this.initButtonListeners();
        this.findAudioManager();
        
        // 播放主菜单背景音乐
        if (this.audioManager) {
            this.audioManager.playMusic('menu_bgm');
        }
    }
    
    onDestroy() {
        this.removeButtonListeners();
    }
    
    /**
     * 查找AudioManager实例
     */
    private findAudioManager(): void {
        if (!this.audioManager) {
            // 尝试在场景中查找AudioManager
            const audioManagerNode = director.getScene().getChildByName('AudioManager');
            if (audioManagerNode) {
                this.audioManager = audioManagerNode.getComponent(AudioManager);
            }
            
            if (!this.audioManager) {
                console.warn('未找到AudioManager引用，音频功能可能无法正常工作');
            }
        }
    }
    
    /**
     * 初始化UI状态
     */
    private initUI(): void {
        // 确保面板初始隐藏
        if (this.settingsPanel) {
            this.settingsPanel.active = false;
        }
        
        if (this.aboutPanel) {
            this.aboutPanel.active = false;
        }
    }
    
    /**
     * 初始化按钮监听
     */
    private initButtonListeners(): void {
        if (this.startGameButton && this.startGameButton.node) {
            this.startGameButton.node.on(Button.EventType.CLICK, this.onStartGameButtonClick, this);
        } else {
            console.warn('开始游戏按钮未设置或无效');
        }
        
        if (this.settingsButton && this.settingsButton.node) {
            this.settingsButton.node.on(Button.EventType.CLICK, this.onSettingsButtonClick, this);
        }
        
        if (this.aboutButton && this.aboutButton.node) {
            this.aboutButton.node.on(Button.EventType.CLICK, this.onAboutButtonClick, this);
        }
        
        if (this.quitButton && this.quitButton.node) {
            this.quitButton.node.on(Button.EventType.CLICK, this.onQuitButtonClick, this);
        }
        
        // 设置面板上的按钮
        if (this.settingsPanel) {
            const closeBtn = this.settingsPanel.getChildByName('CloseButton');
            if (closeBtn) {
                closeBtn.on(Button.EventType.CLICK, this.onCloseSettingsButtonClick, this);
            }
        }
        
        // 关于面板上的按钮
        if (this.aboutPanel) {
            const closeBtn = this.aboutPanel.getChildByName('CloseButton');
            if (closeBtn) {
                closeBtn.on(Button.EventType.CLICK, this.onCloseAboutButtonClick, this);
            }
        }
    }
    
    /**
     * 移除按钮监听
     */
    private removeButtonListeners(): void {
        if (this.startGameButton && this.startGameButton.node) {
            this.startGameButton.node.off(Button.EventType.CLICK, this.onStartGameButtonClick, this);
        }
        
        if (this.settingsButton && this.settingsButton.node) {
            this.settingsButton.node.off(Button.EventType.CLICK, this.onSettingsButtonClick, this);
        }
        
        if (this.aboutButton && this.aboutButton.node) {
            this.aboutButton.node.off(Button.EventType.CLICK, this.onAboutButtonClick, this);
        }
        
        if (this.quitButton && this.quitButton.node) {
            this.quitButton.node.off(Button.EventType.CLICK, this.onQuitButtonClick, this);
        }
        
        // 设置面板上的按钮
        if (this.settingsPanel) {
            const closeBtn = this.settingsPanel.getChildByName('CloseButton');
            if (closeBtn) {
                closeBtn.off(Button.EventType.CLICK, this.onCloseSettingsButtonClick, this);
            }
        }
        
        // 关于面板上的按钮
        if (this.aboutPanel) {
            const closeBtn = this.aboutPanel.getChildByName('CloseButton');
            if (closeBtn) {
                closeBtn.off(Button.EventType.CLICK, this.onCloseAboutButtonClick, this);
            }
        }
    }
    
    /**
     * 开始游戏按钮点击处理
     */
    private onStartGameButtonClick(): void {
        console.log("点击开始游戏按钮");
        
        // 播放按钮音效
        if (this.audioManager) {
            this.audioManager.playSound('button_click');
        }
        
        // 加载游戏场景
        director.loadScene(this.gameSceneName, (err) => {
            if (err) {
                console.error('加载游戏场景失败:', err);
            } else {
                console.log('成功加载游戏场景');
            }
        });
    }
    
    /**
     * 设置按钮点击处理
     */
    private onSettingsButtonClick(): void {
        console.log("点击设置按钮");
        
        // 播放按钮音效
        if (this.audioManager) {
            this.audioManager.playSound('button_click');
        }
        
        // 显示设置面板
        if (this.settingsPanel) {
            this.settingsPanel.active = true;
        }
    }
    
    /**
     * 关闭设置面板按钮点击处理
     */
    private onCloseSettingsButtonClick(): void {
        console.log("点击关闭设置面板按钮");
        
        // 播放按钮音效
        if (this.audioManager) {
            this.audioManager.playSound('button_click');
        }
        
        // 隐藏设置面板
        if (this.settingsPanel) {
            this.settingsPanel.active = false;
        }
    }
    
    /**
     * 关于按钮点击处理
     */
    private onAboutButtonClick(): void {
        console.log("点击关于按钮");
        
        // 播放按钮音效
        if (this.audioManager) {
            this.audioManager.playSound('button_click');
        }
        
        // 显示关于面板
        if (this.aboutPanel) {
            this.aboutPanel.active = true;
        }
    }
    
    /**
     * 关闭关于面板按钮点击处理
     */
    private onCloseAboutButtonClick(): void {
        console.log("点击关闭关于面板按钮");
        
        // 播放按钮音效
        if (this.audioManager) {
            this.audioManager.playSound('button_click');
        }
        
        // 隐藏关于面板
        if (this.aboutPanel) {
            this.aboutPanel.active = false;
        }
    }
    
    /**
     * 退出游戏按钮点击处理
     */
    private onQuitButtonClick(): void {
        console.log("点击退出游戏按钮");
        
        // 播放按钮音效
        if (this.audioManager) {
            this.audioManager.playSound('button_click');
        }
        
        // 在Web平台上，显示确认对话框
        if (confirm('确定要退出游戏吗？')) {
            // 在Native平台上，可以直接退出应用
            // 在Web平台上，可以跳转到特定URL或关闭窗口
            console.log('游戏退出');
            
            // 如果在原生平台，可以使用JSB调用原生API退出
            // 此处只做日志记录
        }
    }
} 